package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.Equipment;
import com.hgxd.entity.vo.pagevo.EquipmentVo;
import com.hgxd.result.Result;

import com.hgxd.service.IEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备表 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "设备表")
@RestController
@RequestMapping("/api/equipment")
@CrossOrigin
public class EquipmentController {
    @Resource(name = "EquipmentServiceImpl")
    private IEquipmentService equipmentService;

    /**
     * 根据组织名字来获取最新的设备信息
     */
    @PostMapping("/getNewDataByOrganizationName/{organizationName}")
    @ApiOperation(value = "根据组织名字来获取最新的设备信息", notes = "根据组织名字来获取最新的设备信息")
    @ApiImplicitParam(name = "organizationName", value = "组织名称")
    @OperationLogAnnotation(operModule = "设备表-根据组织名字来获取最新的设备信息", operType = "获取", operContent = "根据组织名字来获取最新的设备信息")
    public Result<List<Equipment>> getNewDataByOrganizationName(@PathVariable String organizationName) {
        List<Equipment> equipment = equipmentService.getNewDataByOrganizationName(organizationName);
        return Result.ok(equipment);
    }

    /**
     * 根据organizationId查询对应设备
     *
     * @param organizationId
     * @return
     */
    @ApiOperation(value = "根据organizationId查询对应设备", notes = "传入对应的组织id(organizationId)")
    @OperationLogAnnotation(operModule = "设备-获取全部的设备组织", operType = "查询", operContent = "根据organizationId查询对应设备")
    @GetMapping("/getEquipmentsByOrganizationId/{organizationId}")
    public Result<List<Equipment>> getEquipmentsByOrganizationId(@PathVariable("organizationId") Integer organizationId) {
        return Result.ok(equipmentService.getEquipmentsByOrganizationId(organizationId));
    }

    /**
     * 获取全部的设备组织
     */
    @GetMapping("/getAllOrganization")
    @ApiOperation(value = "获取全部的设备组织", notes = "获取全部的设备组织")
    @OperationLogAnnotation(operModule = "设备-获取全部的设备组织", operType = "获取", operContent = "获取全部的设备组织")
    public Result<List<Map<String, Object>>> getAllOrganization() {
        List<Map<String, Object>> getAllOrganization = equipmentService.getAllOrganization();
        return Result.ok(getAllOrganization);
    }

    /**
     * 获取当前设备在线率（大屏用）
     */
    @GetMapping("/getOrganizationOnlineRate")
    @ApiOperation(value = "获取当前设备在线率", notes = "获取当前设备在线率")
    @OperationLogAnnotation(operModule = "设备-获取当前设备在线率", operType = "获取", operContent = "获取当前设备在线率")
    public Result<String> getOrganizationOnlineRate() {
        Double getOrganizationOnlineRate = Double.valueOf(equipmentService.getOrganizationOnlineRate());
        List<Equipment> list = equipmentService.list();
        Double rate = getOrganizationOnlineRate / list.size() * 100;
        DecimalFormat df = new DecimalFormat("#0.00");
        Double result = Double.valueOf(df.format(rate));
        return Result.ok(result.toString().concat("%"));
    }

    /**
     * 获取当前设备在线数量（大屏用）
     */
    @GetMapping("/getCountOrganizationOnline")
    @ApiOperation(value = "获取当前设备在线数量", notes = "获取当前设备在线数量")
    @OperationLogAnnotation(operModule = "设备-获取当前设备在线数量", operType = "获取", operContent = "获取当前设备在线数量")
    public Result<Integer> getCountOrganizationOnline() {
        Integer getOrganizationOnlineRate = equipmentService.getOrganizationOnlineRate();
        return Result.ok(getOrganizationOnlineRate);
    }

    /**
     * 获取当前设备离线数量（大屏用）
     */
    @GetMapping("/getCountOrganizationNotOnline")
    @ApiOperation(value = "获取当前设备离线数量", notes = "获取当前设备离线数量")
    @OperationLogAnnotation(operModule = "设备-获取当前设备离线数量", operType = "获取", operContent = "获取当前设备离线数量")
    public Result<Integer> getCountOrganizationNotOnline() {
        Integer getOrganizationOnlineRate = equipmentService.getOrganizationOnlineRate();
        int size = equipmentService.list().size();
        int i = size - getOrganizationOnlineRate;
        return Result.ok(i);
    }

    /**
     * 查询全部设备的总数(大屏用）
     */
    @GetMapping("/getCountEquipment")
    @ApiOperation(value = "查询全部设备的总数", notes = "查询全部设备的总数")
    @OperationLogAnnotation(operModule = "设备-查询全部设备的总数", operType = "查询", operContent = "查询全部设备的总数")
    public Result<Integer> getCountEquipment() {
        return Result.ok(equipmentService.list().size());
    }

    /**
     * 查询全部设备的种类(大屏用）
     */
    @GetMapping("/getCountEquipmentType")
    @ApiOperation(value = "查询全部设备的种类", notes = "查询全部设备的种类")
    @OperationLogAnnotation(operModule = "设备-查询全部设备的种类", operType = "查询", operContent = "查询全部设备的种类")
    public Result<Integer> getCountEquipmentType() {
        return Result.ok(equipmentService.getCountEquipmentType());
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增设备", notes = "新增设备")
    @ApiImplicitParam(name = "equipment", value = "设备")
    @OperationLogAnnotation(operModule = "设备-新增", operType = "新增", operContent = "新增设备")
    public Result<String> insert(@RequestBody Equipment equipment) {
        boolean save = equipmentService.save(equipment);
        return save ? Result.ok("新增成功") : Result.fail("新增失败");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改设备信息", notes = "修改设备信息")
    @ApiImplicitParam(name = "equipment", value = "设备")
    @OperationLogAnnotation(operModule = "设备-更新", operType = "更新", operContent = "更新设备")
    public Result<String> update(@RequestBody Equipment equipment) {
        boolean data = equipmentService.updateById(equipment);
        return data ? Result.ok("修改成功") : Result.fail("修改失败");
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除设备", notes = "根据id删除设备")
    @ApiImplicitParam(name = "id", value = "id")
    @OperationLogAnnotation(operModule = "设备-删除", operType = "删除", operContent = "删除设备")
    public Result<String> delete(@PathVariable Integer id) {
        boolean data = equipmentService.removeById(id);
        return data ? Result.ok("删除成功") : Result.fail("删除失败");
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除设备", notes = "批量删除设备")
    @ApiImplicitParam(name = "ids", value = "id的数组")
    @OperationLogAnnotation(operModule = "设备-批量删除", operType = "删除", operContent = "批量删除设备")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        boolean data = equipmentService.removeByIds(ids);
        return data ? Result.ok("删除成功") : Result.fail("删除失败");
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部设备", notes = "查询全部设备")
    @OperationLogAnnotation(operModule = "设备-查询全部", operType = "查询全部", operContent = "查询全部设备")
    public Result<List<Equipment>> findAll() {
        return Result.ok(equipmentService.list());
    }


    /**
     * 分页模糊查询 可以根据设备名称、所属组织、设备品牌进行模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询 可以根据设备名称、所属组织、设备品牌进行模糊查询", notes = "分页模糊查询 可以根据设备名称、所属组织、设备品牌进行模糊查询")
    @ApiImplicitParam(name = "equipmentVo", value = "模糊分页所需要的工具类")
    @OperationLogAnnotation(operModule = "设备-分页模糊查询", operType = "分页模糊查询", operContent = "分页模糊查询设备")
    public Result<Page<Equipment>> findPage(@RequestBody EquipmentVo equipmentVo) {
        LambdaQueryWrapper<Equipment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!"".equals(equipmentVo.getEquipmentName()), Equipment::getEquipmentName, equipmentVo.getEquipmentName());
        lambdaQueryWrapper.eq(!"".equals(equipmentVo.getEquipmentType()), Equipment::getEquipmentType, equipmentVo.getEquipmentType());
        lambdaQueryWrapper.eq(!"".equals(equipmentVo.getOrganizationName()), Equipment::getOrganizationName, equipmentVo.getOrganizationName());
        lambdaQueryWrapper.eq(Equipment::getIsdelete, "0");
        return Result.ok(equipmentService.page(new Page<>(equipmentVo.getPageNum(), equipmentVo.getPageSize()), lambdaQueryWrapper));
    }
}

